<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\finance\model;

use  app\finance\model\Model;
use  app\finance\model\Common;

class Recharge extends Model{
	
    private $account = 'user_account'; // 账户表
    private $recharge = 'user_recharge'; // 充值表

    /**
     * 充值列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //列表
        $list['datalist']= Model::name($this->recharge)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->field('a.*,b.realname,b.username')
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->where($where)
            ->select();
        //数据总数
        $list['total']=Model::name($this->recharge)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 充值信息
     * @param array $recharge 
     * @param array $where 
     */ 
    public function recharge($id=null){  
         return Model::name($this->recharge)
           ->alias('a')
           ->join($this->account.' b','a.aid=b.aid','left')
           ->field('a.*, b.realname,b.mobile,b.username,b.email')
           ->where(['a.id'=>$id])
           ->find();
    }

    /**
     * 充值保存
     * @param array $save 
     * @param array $where 
    */ 
    public function save($data,$operator=[]){
        //保存
       $data  = [
            'aid'=>$data['aid'],
            'type'=>$data['type'],
            'method'=>$data['method'],
            'money'=>$data['money'],
            'remoney'=>$data['remoney'],
            'integra'=>$data['integra'],
            'reintegra'=>$data['reintegra'],
            'currency'=>$data['currency'],
            'rate'=>$data['rate'],
            'codes'=>$data['codes'],
            'file_url'=>$data['file_url'],
            'status'=>$data['status'],
            'createtime'=>date('Y-m-d H:i:s')
        ];
        $amount=0;$integra=0;$username='';
        if(!empty($data['aid'])){
            $user = (new Common())->getAccount('aid='.$data['aid'],'pid, amount, integra,username');
            $data['pid'] = $user['pid']; 
            $amount = $user['amount'];
            $integra = $user['integra'];
            $username = $user['username'];  
        }else{
            $data['pid'] = 0; 
        }
        Model::startTrans();
        try {
            if($data['type']==2){
                $data['reintegra']=0;
                $msg='模拟充值'; 
            }else{
                $msg='交易充值'; 
            }
            Model::name($this->recharge)->insert($data);
            Model::commit();
            logrecord(
                '账户id'.$data['aid'].';账户'.$username.'; 账户金额：'.($amount+$integra).';赠送:'.$data['integra'].';交易金额：'.$data['money'].';操作：'.$msg,
                $operator,
                'recharge/'
            );
            return true;
        } catch (Exception $e) {
            Model::rollback();
            return false;  
        }
        return false;
    }

    /**
     * 充值審核
     * @param array $examine 
     * @param array $where 
    */ 
    public function examine($data,$operator=[]){
        $id=$data['id'];
        //保存
        $data  = [
            'aid'=>$data['aid'],
            'type'=>$data['type'],
            'reintegra'=>isset($data['reintegra'])?$data['reintegra']:0,
            'remoney'=>$data['remoney'],
            'notes'=>$data['notes'],
            'status'=>$data['status'],
            'updatetime'=>date('Y-m-d H:i:s')
        ];
        $amount=(new Common())->getAccount('aid='.$data['aid'],'amount,giftamount,integra,username');
        switch ($data['status']) {
            case 3:
                Model::startTrans();
                try {
                    switch ($data['type']) {
                        case 1:
                            (new Common())->updateAccount('aid='.$data['aid'],['amount'=>($amount['amount']+$data['remoney']),'integra'=>($amount['integra']+$data['reintegra'])]);
                            break;
                        case 2:
                            (new Common())->updateAccount('aid='.$data['aid'],['giftamount'=>($amount['giftamount']+$data['remoney'])]);
                            break;
                    }
                    logrecord(
                        '记录id'.$id.';账户'.$amount['username'].'; 账户金额：'.($amount['amount']+$amount['integra']).';赠送:'.$data['reintegra'].';交易金额：'.$data['remoney'].';审核：通过',
                        $operator,
                        'recharge/'
                    );
                    unset($data['aid'], $data['type']);
                    Model::name($this->recharge)->where('id=' . $id)->update($data);
                    Model::commit();
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                return true;
            default:
                Model::startTrans();
                try {
                    if($data['status']==2){
                        $msg='退回'; 
                    }else{
                        $msg='无审核'; 
                    }
                    logrecord(
                        '记录id'.$id.';账户'.$amount['username'].'; 账户金额：'.($amount['amount']+$amount['integra']).';赠送:'.$data['reintegra'].';交易金额：'.$data['remoney'].';审核：'.$msg,
                        $operator,
                        'recharge/'
                    );
                    unset($data['aid'], $data['type'],$data['reintegra']);
                    Model::name($this->recharge)->where('id=' . $id)->update($data);
                    Model::commit();
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                return true;
        }
        return false;
    }

}
